print(Sys.time())

options(stringsAsFactors=F)

suppressPackageStartupMessages(library(tidyr))
suppressPackageStartupMessages(library(igraph))
suppressPackageStartupMessages(library(plyr))
suppressPackageStartupMessages(library(dplyr))


load(
    file="data/en_tweets_2016-01-01_to_2016-11-08_dimensions_election16_remove_news_docs_only_combined_replicate_psrm.RData"
)


all_docs_by_mention_single <- subset(
    all_docs,
    !is.na(user_mentions) & !grepl(",", user_mentions)
) %>%
    mutate(user_mentions_unlisted = unlist(user_mentions_unlisted))

## in warning message here new names are for the discarded list
all_docs_by_mention_multiple <- subset(
    all_docs,
    !is.na(user_mentions) & grepl(",", user_mentions)
) %>%
    unnest(user_mentions_unlisted)

all_docs_by_mention <- rbind(
    all_docs_by_mention_single,
    all_docs_by_mention_multiple
)

troll_ids <- unique(all_docs$userid)

library(igraph)
A <- as_adj(graph.edgelist(
    as.matrix(
        subset(
            all_docs_by_mention,
            not_holdout_alt &
            substr(tweet_time, 1, 10) >= "2016-01-01"
            & user_mentions_unlisted != 0 # userid 0 ?
        )[,c("userid","user_mentions_unlisted")],
        )))
A <- A[rownames(A) %in% troll_ids,]
the_sum <- Matrix::colSums(A)
the_sum[the_sum==0] <- 1
A <- sweep(A, 2, the_sum, `/`)

g <- graph_from_adjacency_matrix(Matrix::tcrossprod(A), mode="undirected", weighted=T)

b <- g
b <- as.undirected(simplify(
    b,
        edge.attr.comb=list(weight="sum")
))

E(b)$weight_orig <- E(b)$weight

set.seed(987654321)
the_communities <- membership(cluster_fast_greedy(b, weights = E(b)$weight_orig))

E(b)$weight <- log(E(b)$weight_orig+1)

V(b)$community <- factor(the_communities)
## V(b)$color <- V(b)$community

V(b)$color <- "gray"
V(b)$color[V(b)$name %in% subset(all_docs, account_category=="HashtagGamer")$userid] <- "green3"
V(b)$color[V(b)$name %in% subset(all_docs, account_category=="RightTroll")$userid] <- "red"
V(b)$color[V(b)$name %in% subset(all_docs, account_category=="LeftTroll")$userid] <- "blue"
V(b)$color[V(b)$name %in% subset(all_docs, account_category=="NewsFeed")$userid] <- "orange"
V(b)$color[V(b)$name %in% subset(all_docs, account_category=="Commercial")$userid] <- "yellow3"
V(b)$color[V(b)$name %in% subset(all_docs, account_category=="Unknown")$userid] <- "gray"
V(b)$color[V(b)$name %in% subset(all_docs, account_category=="Fearmonger")$userid] <- "black"
V(b)$color[V(b)$name %in% subset(all_docs, is.na(account_category))$userid] <- "pink"

ib <- induced_subgraph(b, log(graph.strength(b)+1) > quantile(log(graph.strength(b)+1), 0.5))
my_plot_layout <- layout_nicely(ib)

ib1 <- ib
ib2 <- ib

## png("figs/network_plot_1.png", width=1000, height=1000)
pdf("figs/figureA17_network_plot_replicate_psrm.pdf", width=20, height=10)
par(mfrow=c(1,2))


## ## most common category
## community_account_categories <- as.matrix(table(V(b)$community, V(b)$color))
## for (i in 1:nrow(community_account_categories)) {
##     V(ib2)$color[V(ib2)$community == i] <- colnames(compmunity_account_categories)[which.max(community_account_categories[i,])]
## }

## ## most common community
V(ib2)$color <- "white"
V(ib2)$color[V(ib2)$community %in% names(which.max(as.matrix(table(V(b)$community, V(b)$color))[,"red"]))] <- "red"
V(ib2)$color[V(ib2)$community %in% names(which.max(as.matrix(table(V(b)$community, V(b)$color))[,"blue"]))] <- "blue"
V(ib2)$color[V(ib2)$community %in% names(which.max(as.matrix(table(V(b)$community, V(b)$color))[,"green3"]))] <- "green3"
V(ib2)$color[V(ib2)$community %in% names(which.max(as.matrix(table(V(b)$community, V(b)$color))[,"gray"]))] <- "gray"


## my_plot_layout <- layout_nicely(ib2)
## ## png("./figs/network_plot_2.png")
plot(
    ib2,
    edge.width=NA,
    vertex.label=NA,
    vertex.size= sqrt(graph.strength(ib2)+1),
    layout=my_plot_layout
    ## vertex.color=V(ib2)$color
)
title("Community Detection",cex.main=2)

plot(
    ib1,
    edge.width=NA,
    vertex.label=NA,
    vertex.size= sqrt(graph.strength(ib1)+1),
    layout=my_plot_layout
)
title("Hand Labels",cex.main=2)

dev.off()


print(Sys.time())
